package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.prd.dto.OaPersonnelRequirementDTO;
import com.prd.enums.OaPersonRequireStatusTypeEnum;
import com.prd.model.OaAppointmentOrganizeDO;
import com.prd.model.OaPersonnelRequirementDO;
import com.prd.mapper.OaPersonnelRequirementMapper;
import com.prd.service.OaPersonnelRequirementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.util.JsonData;
import com.prd.util.StringUtils;
import com.prd.vo.OaPersonRequireVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 人员需求管理数据表 服务实现类
 * </p>
 *
 * @author lizhaoyi
 * @since 2024-01-26
 */
@Service
public class OaPersonnelRequirementServiceImpl extends ServiceImpl<OaPersonnelRequirementMapper, OaPersonnelRequirementDO> implements OaPersonnelRequirementService {


    @Autowired
    private OaPersonnelRequirementMapper requirementMapper;

    /**
     * 人员需求分页列表
     * @param vo
     * @return
     */
    @Override
    public JsonData getPageList(OaPersonRequireVo vo) {

        Page<OaPersonnelRequirementDO> page = new Page<>(vo.getPage(), vo.getSize());
        Page<OaPersonnelRequirementDO> organizeDOPage = requirementMapper.selectPage(page, new QueryWrapper<OaPersonnelRequirementDO>()
                .eq(vo.getStatus() != null, "status", vo.getStatus())
                .eq(StringUtils.isNotEmpty(vo.getDept()), "dept", vo.getDept())
                .eq(vo.getStartDate() != null,"start_time",vo.getStartDate()));

        Map<String,Object> resultMap = new HashMap<>(3);
        resultMap.put("total",organizeDOPage.getTotal());
        resultMap.put("pages",organizeDOPage.getPages());
        resultMap.put("rows",organizeDOPage.getRecords());

        return JsonData.buildSuccess(resultMap);
    }

    /**
     * 发布需求
     * @param requirementDTO
     * @return
     */
    @Override
    public JsonData releaseRequire(OaPersonnelRequirementDTO requirementDTO) {

        // TODO 确认需求后开发
        return null;
    }

    /**
     * 取消发布
     * @param id
     * @return
     */
    @Override
    public JsonData cancelRelease(Long id) {

        OaPersonnelRequirementDO requirementDO = requirementMapper.selectById(id);

        int update = requirementMapper.update(requirementDO, new LambdaUpdateWrapper<OaPersonnelRequirementDO>()
                .set(OaPersonnelRequirementDO::getStatus, OaPersonRequireStatusTypeEnum.APPOINTMENT_ORGANIZE.getCode()));
        return update > 0 ? JsonData.buildSuccess() : JsonData.buildError("取消发布操作异常");
    }

    /**
     * 人员需求详情
     * @param id
     * @return
     */
    @Override
    public JsonData requireDetail(Long id) {
        return JsonData.buildSuccess(requirementMapper.selectById(id));
    }
}
